/**
 * Created by seeker910 on 2017/6/17.
 */
Rsd.define('Rsd.zen.dialog.GuiderDialog', {
    extend: 'Rsd.container.Dialog',
    requires: [
        'Rsd.control.Svg', 
        'Rsd.form.Input',
        'Rsd.container.GuiderContainer',
    ],
    xtype: 'guider-dialog',
    layout:'vbox',
    height:600,
    width:800,
    title:'操作向导',
    header:
    {
        style:{
            color:'#FFF',
            backgroundColor:'#03455dd6' 
        }
    },
    autoLoad:true,
    currentStep:-1,
    startIcon:'icon.start',
    endIcon:'icon.end',
    /**
     * @description 操作步骤
     * {
     *      xtype:'guider-form'
     * }
     */
    //steps:[],
    items:[
        {
            xtype:'container',
            header:false,
            height:50,
            width:'100%',
            layout:'hbox',
            items:[],
            style:{
                    fontSize:'120%',
                    backgroundColor:'#00171eab',
                    color:'#fff',
                    textAlign: 'center'
                }
        },
        { 
            xtype: 'guider-container',
            header:false,
            flex:10,
            width:'100%'
        },
        {
            xtype:'container',
            header:false,
            height:50,
            width:'100%',
            style:{display:'flex',alignItems:'center'},
            items:[
                {xtype:'input',label:false,inputType:'button',text:'Back',width:100,handler:'btn_back',margin:'0 0 0 20',style:{textAlign:'center'}},
                {xtype:'input',label:false,inputType:'button',width:100,handler:'btn_next',margin:'0 20 0 0',style:{position:'absolute',right:'0px',textAlign:'center'}}
            ]
        }
    ],
    /*
     *
     * */
    constructor: function GuiderDialog(config) {

        config = config || {};
        this.apply(config);
    },
    /**
     * 
     */
    onBeforeInit:function onBeforeInit() {
        //debugger;
        var step_flow=[{xtype:'svg',height:50,flex:1,dataSource:this.startIcon,label:{content:'[开始]',position:'bottom',space:0}}];
       
        for(var i in this.items[1].items)
        {
            var step = Number(i)+1; 
            this.items[1].items[i].dialog = this;
            var svg_ds = this.items[1].items[i].icon||'';
            var txt = this.items[1].items[i].title||stepTitle || ('第'+step+'步');
            step_flow.push({xtype:'svg',height:50,flex:1,dataSource:svg_ds,label:{content:txt,position:'bottom',space:0}}); 
        }
        step_flow.push({xtype:'svg',height:50,flex:1,dataSource:this.endIcon,label:{content:'[结束]',position:'bottom',space:0}})
       
        this.items[0].items = step_flow;

        this.callParent(); 
    },
    /**
     * 
     */
    load:function load()
    { 
        this.callParent();
        this.setStep(0);
    },
    /**
     * 
     * @returns 
     */
    btn_back:function btn_back() {

        if(this.currentStep == 0)
        {
            alert('已经是第一步。')
            return;
        }
        else
        {
            //debugger;
            var _flag = true;
            var _guider = this.items[1].items[this.currentStep];
            if(Rsd.isFunction(_guider.previousStep.fn))
            {
                _flag = _guider.previousStep.fn.call(_guider); 
            }
            else
            {
                 _flag = _guider.funApplyByIOC(_guider.previousStep.fn,[_guider]);
            }

            if(_flag)
            {
                this.setStep(this.currentStep-1);
            }

        }
    },
    /**
     * 
     * @returns 
     */
    btn_next:function btn_next() {

        if(this.currentStep < this.items[1].items.length )
        {
            var _flag = true;
            var _guider = this.items[1].items[this.currentStep];
            
            if(Rsd.isFunction(_guider.nextStep.fn))
            {
                _flag = _guider.nextStep.fn.call(_guider); 
            }
            else
            {
                _flag = _guider.funApplyByIOC(_guider.nextStep.fn,[_guider]);
            }
            //console.log(_guider.getData());
            if(_flag)
            {
                this.setStep(this.currentStep+1);
            }


        }else
        {
            alert('已经是最后一步。')
            return ;
        } 

    },
    /**
     * 
     * @param {*} step 
     * @returns 
     */
    setStep:function setStep(step) {
        var _guider = null;
        if(!this.isRendered())
        {
            alert('请在GuiderDialog展现后设置步骤值。')
            return _guider;
        }
        var _step = step||0;
        if(_step < this.items[1].items.length)
        {
            if(this.currentStep<0)
            {

            }else
            {
                this.items[1].items[this.currentStep].hide();
            }
             _guider = this.items[1].items[_step];
            _guider.show(); 

            this.currentStep = _step;
            this.items[2].items[0].setValue(_guider.previousStep.text||'-');
            this.items[2].items[1].setValue(_guider.nextStep.text||'-')

            if(_guider.previousStep.visible)
            {
                this.items[2].items[0].show();
            }
            else
            {
                this.items[2].items[0].hide();
            }
            if(_guider.nextStep.visible)
            {
                this.items[2].items[1].show();
            }
            else
            {
                this.items[2].items[1].hide();
            }
            //加载页面数据
            if(_guider.autoLoad)
            {
                Rsd.showWaiting(_guider.id);
                var _fn_close = function(dialog){ 
                   Rsd.closeWaiting(_guider.id); 
                };
                try
                {
                    _guider.funApplyByIOC('load',[_fn_close]);
                }
                catch(err)
                {
                    console.error(err);
                    Rsd.closeWaiting(_guider.id); 
                }
               
            }
 
        }
        else
        {
            alert('step值大于步骤最大值。');
        }
        return _guider;

    },
    /**
     * 
     * @param {*} step 
     * @returns 
     */
    getStep:function getStep(step)
    {
        if(step < 0)
        {
            alert('step值应大于0。');
            return null;
        }
        if(step < this.items[1].items.length)
        {
            return  this.items[1].items[step];
        }
        else
        {
            alert('step值大于步骤最大值。');
            return null;
        } 

    }

},function (type) {

    var _stepsGetter = function () {
 
        return this.items[1].items;

    };

    var _stepsSetter = function (value) { 
            if(Rsd.isArray(value))
            {
                this.items[1].items = value;
            }
            else
            {
                this.items[1].items = [value];
            }
            
        }

    this.defineProperty(type, "steps", _stepsGetter, _stepsSetter, true);
});